package com.budwk.app.util.poiUtils.fireHiddenOffice.impl;

import cn.hutool.core.bean.BeanUtil;
import com.budwk.app.sqfw.dto.FireHiddenCheckWordDTO;
import com.budwk.app.sqfw.dto.FireHiddenOfficeWordDTO;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.enums.WeiQingMenPdfType;
import com.budwk.app.sqfw.models.Sqfw_Sms_Msg;
import com.budwk.app.sqfw.models.Sqfw_formSummary;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.budwk.app.util.ThreadPoolExecutorUtil;
import com.budwk.app.util.poiUtils.WeiQingMenStreetFireHiddenOfficeUtils;
import com.budwk.app.util.poiUtils.WeiQingMenStreetWordTemplate;
import com.budwk.app.util.poiUtils.fireHiddenOffice.IFireHiddenOffice;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;

import java.util.Date;
import java.util.List;

/**
 * @author XYD-PC021
 * @project wk-mini
 * @description 威清门
 * @date 2023/11/29 16:18:17
 */
public class WeiQingMenFireHiddenOffice implements IFireHiddenOffice {

    @Override
    public GenerateWordAndPdfUrlVO createOffice(FireHiddenCheckWordDTO wordDTO) {
        FireHiddenOfficeWordDTO fireHiddenOfficeWordDTO = new FireHiddenOfficeWordDTO();
        BeanUtil.copyProperties(wordDTO, fireHiddenOfficeWordDTO);
        //获取上一次的巡查
        Cnd cnd = Cnd.NEW();
        cnd.and("unit_id", "=", fireHiddenOfficeWordDTO.getUnitId())
                .and("a1", "=", fireHiddenOfficeWordDTO.getA1())
                .and("sqfw_id", "=", fireHiddenOfficeWordDTO.getSqfwId())
                .and("fill_date", "<", fireHiddenOfficeWordDTO.getFillDate());
        cnd.orderBy("fill_date", "desc");
        Sqfw_formSummary formSummary = formSummaryService.fetch(cnd);
        FireHiddenOfficeWordDTO upFormSummary = new FireHiddenOfficeWordDTO();
        BeanUtil.copyProperties(formSummary, upFormSummary);
        fireHiddenOfficeWordDTO.setUpFormSummary(formSummary != null ? upFormSummary : null);

        // 生成拆分pdf
        createSplitPdf(fireHiddenOfficeWordDTO);

        //生成固定模板的pdf
        GenerateWordAndPdfUrlVO resultVO = WeiQingMenStreetFireHiddenOfficeUtils.createFireHiddenPDF(fireHiddenOfficeWordDTO);

        //设置电话号码
        resultVO.setPhone(fireHiddenOfficeWordDTO.getA4());
        Sqfw_Sms_Msg smsMsg = new Sqfw_Sms_Msg();
        smsMsg.setSummaryId(fireHiddenOfficeWordDTO.getId());
        smsMsg.setName(fireHiddenOfficeWordDTO.getA1());
        smsMsg.setAddress(fireHiddenOfficeWordDTO.getA2());
        smsMsg.setType(SupportBusinessTypeEnum.SAFETY_CHECK.getName());
        smsMsg.setUserName(fireHiddenOfficeWordDTO.getA3());
        smsMsg.setPhone(fireHiddenOfficeWordDTO.getA4());
        resultVO.setSmsMsg(smsMsg);

        return resultVO;
    }

    @Override
    public void createSplitPdf(FireHiddenOfficeWordDTO fireHiddenOfficeWordDTO) {
        for (int i = 1; i < 15; i ++) {
            int finalI = i;

            // 不生成交通应急台账
            if ("否".equals(fireHiddenOfficeWordDTO.getIsTrafficEmergency())) {
                List<Integer> index = WeiQingMenPdfType.getIdx(WeiQingMenPdfType.TRAFFIC_EMERGENCY_INSPECTION.getCode());
                if (index.contains(i)) continue;
            }

            ThreadPoolExecutorUtil.getInstance().execute(() -> {
                fireHiddenOfficeWordDTO.setIndex(finalI);
                String url = WeiQingMenStreetFireHiddenOfficeUtils.createEmergencyPDF(fireHiddenOfficeWordDTO);
                addPdf(fireHiddenOfficeWordDTO, finalI, url);
            });
        }
    }

    @Override
    public String createWordTemplate(FireHiddenOfficeWordDTO wordDTO) {
        return WeiQingMenStreetWordTemplate.createWordTemplate(wordDTO);
    }

    private static boolean addPdf(FireHiddenOfficeWordDTO fireHiddenOfficeWordDTO, int index, String url) {
        Cnd cnd = Cnd.NEW();
        cnd.and("summary_id", "=", fireHiddenOfficeWordDTO.getId())
                .and("index", "=", index);
        Sqfw_form_summary_pdf pdf = formSummaryPdfService.fetch(cnd);

        if (pdf == null) {
            pdf = new Sqfw_form_summary_pdf();
            pdf.setIndex(index);
            pdf.setUrl(url);
            pdf.setStatus(StringUtils.isBlank(url) ? "1" : "2");
            pdf.setSummaryId(fireHiddenOfficeWordDTO.getId());
            pdf.setCreateDate(fireHiddenOfficeWordDTO.getCreateDate());
            pdf.setUpdateDate(new Date());
            try {
                formSummaryPdfService.insert(pdf);
            } catch (Exception e) {
            }
        } else {
            if (StringUtils.isNotBlank(url)) {
                pdf.setUrl(url);
            }
            pdf.setStatus(StringUtils.isBlank(url) ? "1" : "2");
            pdf.setUpdateDate(new Date());
            formSummaryPdfService.update(pdf);
        }

        return true;
    }
}
